﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DataAccessLayer;

namespace BusinessLogicLayer
{
    public class GiaoViecBLL
    {
        QuanLyChamCongDataContext DB = new QuanLyChamCongDataContext();

        public List<DGVGIAOVIEC> LayDSGiaoViec(int mapb, DateTime _tungay)
        {
            List<DGVGIAOVIEC> _ls = (from a in DB.GIAOVIECs
                                     where a.NHANVIEN.MaPB == mapb && a.TuNgay == _tungay
                                     select new DGVGIAOVIEC()
                                     {
                                         MaNV = (int)a.MaNV,
                                         MaGiaoViec = (int)a.MaGiaoViec,
                                         HoTen = a.NHANVIEN.HoTen,
                                         Email = a.NHANVIEN.Email,
                                         TrangThai = (int)a.TrangThai
                                     }).ToList();
            return _ls;
        }

        public GIAOVIEC LayGiaoViec(int _magv)
        {
            return DB.GIAOVIECs.SingleOrDefault(a => a.MaGiaoViec == _magv);
        }

        /// <summary>
        /// Kiểm tra tồn tại dữ liệu giao việc của phòng ban trong tuần
        /// </summary>
        /// <param name="_tuNgay"></param>
        /// <param name="_maPB"></param>
        /// <returns></returns>
        public bool KiemTraTonTai(DateTime _tuNgay, int _maPB)
        {
            if (DB.GIAOVIECs.Any(a => a.NHANVIEN.MaPB == _maPB && a.TuNgay == _tuNgay))
                return true;
            else
                return false;
        }

        public void Them(GIAOVIEC gv)
        {
            DB.GIAOVIECs.InsertOnSubmit(gv);
            DB.SubmitChanges();
        }

        public void ThemDSGiaoViec(List<NHANVIEN2> _lsNV, DateTime _tuNgay)
        {
            foreach (var i in _lsNV)
            {
                GIAOVIEC g = new GIAOVIEC();
                g.MaNV = i.MaNV;
                g.TuNgay = _tuNgay;
                g.TrangThai = 0;
                g.DenNgay = _tuNgay.AddDays(5);
                Them(g);
            }
        }

        public void Sua(GIAOVIEC n)
        {
            GIAOVIEC o = DB.GIAOVIECs.SingleOrDefault(a => a.MaGiaoViec == n.MaGiaoViec);

            if (n.MaNV == null)
                o.MaNV = o.MaNV;
            else
                o.MaNV = n.MaNV;

            if (n.TuNgay == null || n.DenNgay == null)
            {
                o.TuNgay = o.TuNgay;
                o.DenNgay = o.DenNgay;
            }
            else
            {
                o.TuNgay = n.TuNgay;
                o.DenNgay = n.DenNgay;
            }

            if (n.ChuThich == null) { }
            else
            {
                o.ChuThich = n.ChuThich;
            }

            if (n.TrangThai == null)
            {
                o.TrangThai = o.TrangThai;
            }
            else
            {
                o.TrangThai = n.TrangThai;
            }

            if (n.KetQua == null)
            {
                o.KetQua = o.KetQua;
            }
            else
            {
                o.KetQua = n.KetQua;
            }

            if (n.TrangThaiDoc == null)
            { }
            else
            {
                o.TrangThaiDoc = n.TrangThaiDoc;
            }

            DB.SubmitChanges();
        }

        public int LayMaGiaoViec(DateTime _tungay, int _manv)
        {
            GIAOVIEC n = DB.GIAOVIECs.SingleOrDefault(a => a.TuNgay == _tungay && a.MaNV == _manv);
            return n.MaGiaoViec;
        }

        public void GiaoViec(DateTime _tungay, int _mapb)
        {
            List<GIAOVIEC> _ls = (from a in DB.GIAOVIECs
                                  from b in DB.CT_GIAOVIECs
                                  where a.TuNgay == _tungay && a.NHANVIEN.MaPB == _mapb && a.MaGiaoViec == b.MaGiaoViec
                                  select a).ToList();

            foreach (var i in _ls)
            {
                i.TrangThai = 2;                
            }

            DB.SubmitChanges();
        }

        public List<DGVGIAOVIEC> LayDanhSachGiaoViec(int _manv, int _mapb, int _trangthai)
        {
            List<DGVGIAOVIEC> _ls;
            if (_mapb == 0 && _manv != 0)
            {
                _ls = (from a in DB.GIAOVIECs
                       orderby a.MaGiaoViec descending
                       where a.MaNV == _manv && a.TrangThai == _trangthai
                       select new DGVGIAOVIEC()
                       {
                           MaGiaoViec = a.MaGiaoViec,
                           MaNV = (int)a.MaNV,
                           HoTen = a.NHANVIEN.HoTen,
                           TrangThai = (int)a.TrangThai,
                           Email = a.NHANVIEN.Email,
                           TuNgay = (DateTime)a.TuNgay,
                           DenNgay = (DateTime)a.DenNgay,
                           TrangThaiDoc = a.TrangThaiDoc ?? 1 //hoặc đọc rồi ==1
                       }).ToList();
                return _ls;
            }
            else if (_manv == 0 && _mapb != 0)
            {
                _ls = (from a in DB.GIAOVIECs
                       orderby a.MaGiaoViec descending//ascending
                       where a.NHANVIEN.MaPB == _mapb && a.TrangThai == _trangthai
                       select new DGVGIAOVIEC()
                       {
                           MaGiaoViec = a.MaGiaoViec,
                           MaNV = (int)a.MaNV,
                           HoTen = a.NHANVIEN.HoTen,
                           TrangThai = (int)a.TrangThai,
                           Email = a.NHANVIEN.Email,
                           TuNgay = (DateTime)a.TuNgay,
                           DenNgay = (DateTime)a.DenNgay,
                           TrangThaiDoc = a.TrangThaiDoc ?? 1 //hoặc đọc rồi ==1
                       }).ToList();
                return _ls;
            }
            else 
            {
                return null;
            }            
        }

        public List<GIAOVIEC> LayDSGiaoViecTheoThang(DateTime startDay, DateTime endDay, int manv)
        {
            List<GIAOVIEC> ls = (from a in DB.GIAOVIECs
                                 where a.DenNgay >= startDay && a.DenNgay <= endDay && a.MaNV == manv
                                 select a).ToList();
            return ls;
        }

        public List<GIAOVIEC> LayDSKQCongViecTheoThang(int _Thang, int _Nam, int _MaNV)
        {
            List<GIAOVIEC> ls = (from a in DB.GIAOVIECs
                                 where Convert.ToDateTime(a.DenNgay).Month == _Thang && Convert.ToDateTime(a.DenNgay).Year == _Nam && a.MaNV == _MaNV && a.KetQua != null
                                 select a).ToList();
            return ls;
        }

        public int DemThongBao(int _maNV, int _maPB, int _mattd)
        {
            if (_maPB == 0)
            {
                List<GIAOVIEC> ls = (from a in DB.GIAOVIECs
                                     where a.MaNV == _maNV && a.TrangThai == _mattd && a.TrangThaiDoc == 0
                                     select a).ToList();
                if (ls != null)
                {
                    return ls.Count;
                }
                else
                    return 0;
            }
            else
            {
                List<GIAOVIEC> ls = (from a in DB.GIAOVIECs
                                     where a.NHANVIEN.MaPB == _maPB && a.TrangThai == _mattd
                                     select a).ToList();
                if (ls != null)
                {
                    return ls.Count;
                }
                else
                    return 0;
            }
        }
    }
}
